package org.x4444;

import java.io.IOException;
import java.io.InputStream;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class CommandExecutor {

    public static void main(String[] args) throws JSchException, IOException {
        String command = "hadoop fs -rmr wc-out; hadoop jar work/hdmvn/hdmvn.jar org.x4444.CountJob wc-in wc-out; hadoop fs -ls wc-out; hadoop fs -text wc-out/part-r-00000";

        boolean res = new CommandExecutor().execute(command);
        System.out.println("Result: " + res);
    }

    public boolean execute(String command) throws JSchException, IOException {
        try {
            int exitStatus = -1;
            JSch jsch = new JSch();

            String userHome = System.getProperty("user.home");
            String knownHostsFilename = userHome + "/.ssh/known_hosts";
            System.out.println("Use known_host: " + knownHostsFilename);
            jsch.setKnownHosts(knownHostsFilename);

            String privKeyFilename = userHome + "/.ssh/id_rsa";
            System.out.println("Use private key: " + privKeyFilename);
            jsch.addIdentity(privKeyFilename);

            Session session = jsch.getSession("pivovara", "rkvhdp003.devlab.dev");

            try {
                System.out.println("Session Connecting...");
                session.connect();
                System.out.println("Session Connected");

                ChannelExec channel = (ChannelExec) session.openChannel("exec");

                channel.setCommand(command);
                // channel.setCommand("ls -la");

                channel.setErrStream(System.err);
                InputStream in = channel.getInputStream();

                try {
                    System.out.println("Channel Connecting...");
                    channel.connect();
                    System.out.println("Channel Connected");

                    byte[] tmp = new byte[1024];
                    while (true) {
                        while (in.available() > 0) {
                            int i = in.read(tmp);
                            if (i < 0) {
                                break;
                            }
                            System.out.print(new String(tmp, 0, i));
                        }
                        if (channel.isClosed()) {
                            exitStatus = channel.getExitStatus();
                            System.out.println("exit-status: " + exitStatus);
                            break;
                        }
                        try {
                            Thread.sleep(500);
                        } catch (Exception ee) {
                        }
                    }
                } finally {
                    channel.disconnect();
                }
            } finally {
                session.disconnect();
            }
            return exitStatus == 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
